package com.wenjie.springbootbugdetection.web;

import com.wenjie.springbootbugdetection.model.*;
import com.wenjie.springbootbugdetection.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Yang wenjie
 * @date 2021/7/13 15:27
 * Nice to Meet you!
 */
@Controller
public class AdministratorController {
    @Autowired
    private BugService bugService;
    @Autowired
    private SubmitterService submitterService;
    @Autowired
    private Test_managerService test_managerService;
    @Autowired
    private DevelopmentService developmentService;
    @Autowired
    private ModelService modelService;
    @Autowired
    private AdminService adminService;

    /**
     * Submitter模块
     * @return
     */
    @RequestMapping(value = "/findAllSubmitter")
    public String findAllSubmitter(Model model){
        List<Submitter> submitterList = submitterService.select_All_Submitters();
        model.addAttribute("submitterList",submitterList);
        return "Submitter";
    }

    @RequestMapping(value = "/delete_submitter")
    public String deleteSubmitter(@RequestParam("id") String id){
        Integer ID = Integer.valueOf(id);
        Integer i = submitterService.deleteSubmitter(ID);
        if (i != 1){
            return "wrong";
        }else{
            return "redirect:/findAllSubmitter";
        }
    }

    @RequestMapping(value = "/update_submitter")
    public String updateSubmitter(Model model,@RequestParam("id") String id){
        Submitter submitter = null;
        Integer ID = Integer.valueOf(id);
        submitter =  submitterService.selectSubmitter(ID);
        model.addAttribute("submitter",submitter);
        return "Submitter_do_update";
    }

    @RequestMapping(value = "/do_update_submitter")
    public String updateSubmitter(@ModelAttribute Submitter submitter){
        int i = submitterService.updateASubmitter(submitter);
        if (i > 0){
            return "redirect:/findAllSubmitter";
        }else {
            return "wrong";
        }
    }

    @RequestMapping(value = "/add_submitter")
    public String add_submitter(Model model){
      model.addAttribute("submitter",new Submitter());
      return "do_add_submitter";
    }


    @PostMapping("/doAddSubmitter")
    public String greetingSubmit(@ModelAttribute Submitter submitter) {
        int i  = submitterService.insertASubmitter(submitter);
        if (i > 0){
            return "redirect:/findAllSubmitter";
        }else {
            return "wrong";
        }
    }

    /**
     * TestManager模块
     * @return
     */
    @RequestMapping(value = "/findAllTestManagers")
    public String findAllTestManagers(Model model){
        List<Test_manager> Test_managerList = test_managerService.select_All_Managers();
        model.addAttribute("Test_managerList",Test_managerList);
        return "Test_Manager";
    }

    @RequestMapping(value = "/delete_testManager")
    public String delete_testManager(@RequestParam("id") String id){
        Integer ID = Integer.valueOf(id);
        Integer i = test_managerService.deletetestManager(ID);
        if (i != 1){
            return "wrong";
        }else{
            return "redirect:/findAllTestManagers";
        }
    }

    @RequestMapping(value = "/update_testManager")
    public String update_testManager(Model model,@RequestParam("id") String id){
       Test_manager test_manager = null;
       Integer ID = Integer.valueOf(id);
       test_manager =  test_managerService.selectTestManager(ID);
       model.addAttribute("test_manager",test_manager);
       return "testManager_do_update";
    }

    @RequestMapping(value = "/do_update_testManager")
    public String do_update_testManager(@ModelAttribute Test_manager test_manager){
        int i = test_managerService.updateATestManager(test_manager);
        if (i > 0){
            return "redirect:/findAllTestManagers";
        }else {
            return "wrong";
        }
    }

    @RequestMapping(value = "/add_test_manager")
    public String add_test_manager(Model model){
        model.addAttribute("Test_manager",new Test_manager());
        return "do_add_TestManager";
    }


    @PostMapping("/doAddTestmanager")
    public String doAddTestmanager(@ModelAttribute Test_manager test_manager) {
        int i  = test_managerService.insertATestManager(test_manager);
        if (i > 0){
            return "redirect:/findAllTestManagers";
        }else {
            return "wrong";
        }
    }


    /**
     * Development模块
     * @return
     */
    @RequestMapping(value = "/findAllDevelopment")
    public String findAllDevelopment(Model model){
        List<Development> developmentList = developmentService.select_All_Development();
        model.addAttribute("developmentList",developmentList);
        return "Development";
    }

    @RequestMapping(value = "/delete_Development")
    public String delete_Development(@RequestParam("id") String id){
        Integer ID = Integer.valueOf(id);
        Integer i = developmentService.deleteADevelopment(ID);
        if (i != 1){
            return "wrong";
        }else{
            return "redirect:/findAllDevelopment";
        }
    }

    @RequestMapping(value = "/updateDevelopment")
    public String updateDevelopment(Model model,@RequestParam("id") String id){
       Development development = null;
       Integer ID = Integer.valueOf(id);
        development =  developmentService.selectDevelopment(ID);
       model.addAttribute("development",development);
       return "Development_do_update";
    }

    @RequestMapping(value = "/do_update_Development")
    public String do_update_Development(@ModelAttribute Development development){
        int i = developmentService.updateADevelopment(development);
        if (i > 0){
            return "redirect:/findAllDevelopment";
        }else {
            return "wrong";
        }
    }

    @RequestMapping(value = "/add_Development")
    public String add_Development(Model model){
        model.addAttribute("Development",new Development());
        return "do_add_Development";
    }


    @PostMapping("/doAddDevelopment")
    public String doAddTestmanager(@ModelAttribute Development development) {
        int i  = developmentService.insertADevelopment(development);
        if (i > 0){
            return "redirect:/findAllDevelopment";
        }else {
            return "wrong";
        }
    }

    /**
     * Admin 模块
     * @return
     */
    @RequestMapping(value = "/findAllAdmin")
    public String findAllAdmin(Model model){
        List<Admin> adminList = adminService.select_All_Admins();
        model.addAttribute("adminList",adminList);
        return "Admin";
    }

    @RequestMapping(value = "/delete_Admin")
    public String delete_Admin(@RequestParam("id") String id){
        Integer ID = Integer.valueOf(id);
        Integer i = adminService.deleteAAdmin(ID);
        if (i != 1){
            return "wrong";
        }else{
            return "redirect:/findAllAdmin";
        }
    }

    @RequestMapping(value = "/update_admin")
    public String updateAdmin(Model model,@RequestParam("id") String id){
        Admin admin = null;
        Integer ID = Integer.valueOf(id);
        admin =  adminService.selectAdmin(ID);
        model.addAttribute("admin",admin);
        return "Admin_do_update";
    }

    @RequestMapping(value = "/do_update_Admin")
    public String do_update_Admin(@ModelAttribute Admin admin){
        int i = adminService.updateAAdmin(admin);
        if (i > 0){
            return "redirect:/findAllAdmin";
        }else {
            return "wrong";
        }
    }

    @RequestMapping(value = "/add_admin")
    public String add_admin(Model model){
        model.addAttribute("Admin",new Admin());
        return "do_add_Admin";
    }


    @PostMapping("/doAddAdmin")
    public String doAddAdmin(@ModelAttribute Admin admin) {
        int i  = adminService.insertAAdmin(admin);
        if (i > 0){
            return "redirect:/findAllAdmin";
        }else {
            return "wrong";
        }
    }


    /*****************************以下是工具方法***********************************/
    private Map<String, Object> selectAllModels() {
        Map<String, Object> modelsMap = new HashMap<String, Object>();
        List<Model_type> modelTypeList = modelService.selectAllModels();
        for (Model_type model_type : modelTypeList){
            modelsMap.put(model_type.getModelPackage(),model_type.getModelId());
        }
        return modelsMap;
    }
}
